package com.binguo.service;

import com.binguo.common.util.JsonUtil;
import com.binguo.model.mapper.ShopProductMessageMapper;
import com.binguo.model.model.LogProductImg;
import com.binguo.model.model.Product;
import com.binguo.model.model.ShopProductMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ShopProductMsgService {
    private static Logger logger = LoggerFactory.getLogger(ShopProductMsgService.class);

    public static Integer MSG_DELETE = 0;
    public static Integer MSG_PRODUCT_DELETE = 2;
    public static Integer MSG_ADD = 1;


    public static Integer READ_STATE_UNREAD = 0;
    public static Integer READ_STATE_READED = 1;

    @Resource
    private ShopProductMessageMapper shopProductMessageMapper;

    public void addDeleteProductMessage(Product product){

        try {
            logger.info("addDeleteProductMessage:{}", JsonUtil.toJson(product));

            ShopProductMessage shopProductMessage = new ShopProductMessage();
            shopProductMessage.setShopId(product.getShopid());
            shopProductMessage.setBranchId(product.getStoreid());
            shopProductMessage.setProductId(product.getId());
            shopProductMessage.setCreateTime(new Date());
            shopProductMessage.setMsgType(MSG_PRODUCT_DELETE);
            shopProductMessage.setReadState(READ_STATE_UNREAD);


            shopProductMessageMapper.insertSelective(shopProductMessage);
        } catch (Exception e) {
            logger.error("addDeleteProductMessage error:{}", product);
        }
    }

    public void addDeleteProductImgMessage(LogProductImg logProductImg){

        try {
            logger.info("addDeleteProductImgMessage:{}", JsonUtil.toJson(logProductImg));

            String phrase[] = logProductImg.getCutImg().split("/");
            String imageName = phrase[phrase.length - 1];
            String productOrderNo = imageName.substring(0, imageName.indexOf("."));

            if (productOrderNo.contains("cut")) {
                productOrderNo = productOrderNo.substring(0, productOrderNo.indexOf("_cut"));
            }

            ShopProductMessage shopProductMessage = new ShopProductMessage();
            shopProductMessage.setShopId(logProductImg.getShopId());
            shopProductMessage.setBranchId(logProductImg.getBranchId());
            shopProductMessage.setProductId(logProductImg.getProductId());
            shopProductMessage.setCreateTime(new Date());
            shopProductMessage.setMsgType(MSG_DELETE);
            shopProductMessage.setReadState(READ_STATE_UNREAD);
            shopProductMessage.setProductOrderNo(productOrderNo);


            shopProductMessageMapper.insertSelective(shopProductMessage);
        } catch (Exception e) {
            logger.error("addDeleteProductImgMessage error:{}", logProductImg);
        }
    }

    public void addInsertProductImgMessage(LogProductImg logProductImg){

        try {
            logger.info("addInsertProductImgMessage:{}", JsonUtil.toJson(logProductImg));

            ShopProductMessage shopProductMessage = new ShopProductMessage();
            shopProductMessage.setShopId(logProductImg.getShopId());
            shopProductMessage.setBranchId(logProductImg.getBranchId());
            shopProductMessage.setProductId(logProductImg.getProductId());
            shopProductMessage.setCreateTime(new Date());
            shopProductMessage.setMsgType(MSG_ADD);
            shopProductMessage.setReadState(READ_STATE_UNREAD);
            shopProductMessage.setImgUrl(logProductImg.getImgCutNetUrl());


            shopProductMessageMapper.insertSelective(shopProductMessage);
        } catch (Exception e) {
            logger.error("addInsertProductImgMessage error:{}", logProductImg);
        }
    }
}
